﻿using Numbers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Microsoft.Silverlight.Testing;
namespace t_Numbers
{
    
    
    /// <summary>
    ///This is a test class for t_FlavorSample and is intended
    ///to contain all t_FlavorSample Unit Tests
    ///</summary>
    [TestClass()]
    public class t_FlavorSample
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod]
        public void TestFlavorSample()
        {
            FlavorSample f = new FlavorSample();
            Assert.AreEqual(0.0, f.Total, "Total number of events is zero");
            Assert.AreEqual(0.0, f.Light, "Number of light guys is zero");
            Assert.AreEqual(0.0, f.Charm, "Number of charm guys is zero");
            Assert.AreEqual(0.0, f.Bottom, "Number of bottom guys are zero");
            Assert.AreEqual(0.0, f.LightCharm, "Number of charmlight guy sshould be zero");

            CheckConsistency(f);
        }

        [TestMethod]
        public void CheckLightSetting()
        {
            FlavorSample f = new FlavorSample();
            f.Light = 100;
            Assert.AreEqual(100.0, f.Light, "Expected light to be 100");
            CheckConsistency(f);
        }

        [TestMethod]
        public void CheckCharmSetting()
        {
            FlavorSample f = new FlavorSample();
            f.Charm = 100;
            Assert.AreEqual(100.0, f.Charm, "Expected charm to be 100");
            CheckConsistency(f);
        }

        [TestMethod]
        public void CheckBottomSetting()
        {
            FlavorSample f = new FlavorSample();
            f.Bottom = 100;
            Assert.AreEqual(100.0, f.Bottom, "Expected 100 for bottom");
            CheckConsistency(f);
        }

        [TestMethod]
        public void CheckCharmLightSetting()
        {
            FlavorSample f = new FlavorSample();
            f.LightCharm = 100;
            Assert.AreEqual(50.0, f.Light, "Expected 50-50 split!");
            Assert.AreEqual(50.0, f.Charm, "Expected 50-50 split!");
            CheckConsistency(f);

            f.Charm = 100;
            f.LightCharm = 300;
            Assert.AreEqual(300.0, f.LightCharm, "Expected light/charm to be set to its new value!");
            Assert.AreEqual(100, f.Light, "Expected the fraction to be kept the same!");
            CheckConsistency(f);
        }

        [TestMethod]
        public void CheckTotalSetting()
        {
            FlavorSample f = new FlavorSample();
            f.Total = 300;
            Assert.AreEqual(100, f.Light, "Expected even split");
            Assert.AreEqual(100, f.Bottom, "Expected even split");
            CheckConsistency(f);

            f.Bottom = 200;
            f.Total = 800;
            Assert.AreEqual(800, f.Total, "Expected the total to have been set!");
            Assert.AreEqual(200, f.Light, "Expected light to double");
            CheckConsistency(f);

            f.LightCharm = 200;
            Assert.AreEqual(600.0, f.Total, "The change should have been propagated to the total!");
            CheckConsistency(f);

            f.Total = 0;
            Assert.AreEqual(0, f.Total, "Total should be zero");
            Assert.AreEqual(0, f.Bottom, "Total bottom is zero");
            Assert.AreEqual(0, f.Charm, "total charm should be zero");
            Assert.AreEqual(0, f.Light, "total light should be zero");
        }

        /// <summary>
        /// Some very simple quick checks on the consistency of the thing.
        /// </summary>
        /// <param name="f"></param>
        private void CheckConsistency(FlavorSample f)
        {
            Assert.AreEqual(f.Light + f.Charm, f.LightCharm, "Total up light and charm should match too!");
            Assert.AreEqual(f.Light + f.Charm + f.Bottom, f.Total, "Total of light/charm/bottom dont' match");
        }

        [TestMethod]
        public void TestMultiply()
        {
            FlavorSample fs = new FlavorSample();
            TagEfficiency eff = new TagEfficiency();
            FlavorSample result = new FlavorSample();

            FlavorSample.Multiply(result, eff, fs);

            Assert.AreEqual(0.0, result.Light, "The light should be zero!");

            eff.eff_b = 0.7;
            eff.eff_c = 0.6;
            eff.eff_l = 0.5;
            fs.Bottom = 100;
            fs.Charm = 1000;
            fs.Light = 200;
            Assert.AreEqual(70.0, result.Bottom, "The bottom should have changed!");
            Assert.AreEqual(600.0, result.Charm, "Charm isn't working");
            Assert.AreEqual(100, result.Light, "Light isn't working");
        }

        [TestMethod]
        public void TestPropertyChangedNotification()
        {
            FlavorSample fs = new FlavorSample();
            List<string> lastName = new List<string>();
            fs.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler((o, e) => {
                lastName.Add(e.PropertyName);
            });

            fs.Bottom = 100;
            Assert.AreEqual(2, lastName.Count, "Expected two prop changed guys for b!");
            Assert.IsTrue(lastName.Contains("Bottom"), "expected bottom to have fired for b!");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for b!");
            lastName.Clear();

            fs.Charm = 100;
            Assert.AreEqual(3, lastName.Count, "Expected tree prop changed guys for c!");
            Assert.IsTrue(lastName.Contains("Charm"), "expected bottom to have fired for c!");
            Assert.IsTrue(lastName.Contains("LightCharm"), "expected bottom to have fired for c!");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for c!");
            lastName.Clear();

            fs.Light = 100;
            Assert.AreEqual(3, lastName.Count, "Expected tree prop changed guys for l!");
            Assert.IsTrue(lastName.Contains("Light"), "expected bottom to have fired for l!");
            Assert.IsTrue(lastName.Contains("LightCharm"), "expected bottom to have fired for l!");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for l!");
            lastName.Clear();

            fs.LightCharm = 600;
            Assert.AreEqual(4, lastName.Count, "Expected tree prop changed guys for lc!");
            Assert.IsTrue(lastName.Contains("Light"), "expected bottom to have fired for lc!");
            Assert.IsTrue(lastName.Contains("LightCharm"), "expected bottom to have fired for lc!");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for lc!");
            Assert.IsTrue(lastName.Contains("Charm"), "Expected total to have fired for lc!");
            lastName.Clear();

            fs.Total = 1400;
            Assert.AreEqual(5, lastName.Count, "Expected tree prop changed guys for tot!");
            Assert.IsTrue(lastName.Contains("Light"), "expected bottom to have fired for tot!");
            Assert.IsTrue(lastName.Contains("LightCharm"), "expected bottom to have fired for tot!");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for tot!");
            Assert.IsTrue(lastName.Contains("Charm"), "Expected total to have fired for tot!");
            Assert.IsTrue(lastName.Contains("Bottom"), "Expected total to have fired for tot!");
            lastName.Clear();

            fs.Bottom = 100;
            fs.Charm = 100;
            fs.Light = 100;
            lastName.Clear();

            fs.Bottom = 100;
            fs.Charm = 100;
            fs.Light = 100;
            fs.LightCharm = 200;
            fs.Total = 300;
            Assert.AreEqual(0, lastName.Count, "Setting to same value shouldn't have changed anything!");

            FlavorSample source = new FlavorSample();
            TagEfficiency te = new TagEfficiency();
            te.eff_b = te.eff_c = te.eff_l = 1.0;

            FlavorSample.Multiply(fs, te, source);
            lastName.Clear();

            source.Bottom = 100;
            Assert.AreEqual(2, lastName.Count, "Expected two prop changed guys for b! Value");
            Assert.IsTrue(lastName.Contains("Bottom"), "expected bottom to have fired for b! Value");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for b! Value");
            lastName.Clear();

            source.Charm = 100;
            Assert.AreEqual(3, lastName.Count, "Expected tree prop changed guys for c! Value");
            Assert.IsTrue(lastName.Contains("Charm"), "expected bottom to have fired for c! Value");
            Assert.IsTrue(lastName.Contains("LightCharm"), "expected bottom to have fired for c! Value");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for c! Value");
            lastName.Clear();

            source.Light = 100;
            Assert.AreEqual(3, lastName.Count, "Expected tree prop changed guys for l! Value");
            Assert.IsTrue(lastName.Contains("Light"), "expected bottom to have fired for l! Value");
            Assert.IsTrue(lastName.Contains("LightCharm"), "expected bottom to have fired for l! Value");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for l! Value");
            lastName.Clear();

            source.LightCharm = 600;
            Assert.AreEqual(4, lastName.Count, "Expected tree prop changed guys for lc! Value");
            Assert.IsTrue(lastName.Contains("Light"), "expected bottom to have fired for lc! Value");
            Assert.IsTrue(lastName.Contains("LightCharm"), "expected bottom to have fired for lc! Value");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for lc! Value");
            Assert.IsTrue(lastName.Contains("Charm"), "Expected total to have fired for lc! Value");
            lastName.Clear();

            source.Total = 1400;
            Assert.AreEqual(5, lastName.Count, "Expected tree prop changed guys for tot! Value");
            Assert.IsTrue(lastName.Contains("Light"), "expected bottom to have fired for tot! Value");
            Assert.IsTrue(lastName.Contains("LightCharm"), "expected bottom to have fired for tot! Value");
            Assert.IsTrue(lastName.Contains("Total"), "Expected total to have fired for tot! Value");
            Assert.IsTrue(lastName.Contains("Charm"), "Expected total to have fired for tot! Value");
            Assert.IsTrue(lastName.Contains("Bottom"), "Expected total to have fired for tot! Value");
            lastName.Clear();
        }

        [TestMethod]
        public void TestFractionCollection()
        {
            FlavorSample fc = new FlavorSample();

            Assert.IsNotNull(fc.JetbclFractions, "Expected jet bcl not to be null!");
            Assert.AreEqual(3, fc.JetbclFractions.Count, "Expected to see 3 items in bcl!");
            Assert.AreEqual("b", fc.JetbclFractions[0].Name, "Expected B's first");
            Assert.AreEqual("c", fc.JetbclFractions[1].Name, "Expected C's Second");
            Assert.AreEqual("l", fc.JetbclFractions[2].Name, "Expected L's Third");

            Assert.AreEqual(0.0, fc.JetbclFractions[0].Value, "Expected zero for b's 1");
            Assert.AreEqual(0.0, fc.JetbclFractions[1].Value, "Expected zero for c's 1");
            Assert.AreEqual(0.0, fc.JetbclFractions[2].Value, "Expected zero for l's 1");

            fc.Bottom = 10;
            fc.Charm = 20;
            fc.Light = 30;

            Assert.AreEqual(10.0, fc.JetbclFractions[0].Value, "Expected zero for b's 2");
            Assert.AreEqual(20.0, fc.JetbclFractions[1].Value, "Expected zero for c's 2");
            Assert.AreEqual(30.0, fc.JetbclFractions[2].Value, "Expected zero for l's 2");
        }
    }
}
